package com.zhj.bz.datastructure.stack;

/**
 * 前缀表达式 运算符位于操作数之前 波兰
 * 后缀表达式 运算符位于操作数之后 逆波兰
 * 中缀表达式 **
 * @author zhj
 */
public class Calculator {

    public static void main(String[] args) {
        String expression = "8880-600/6*20";
        ArrayStackPlus nums = new ArrayStackPlus(10);
        ArrayStackPlus opers = new ArrayStackPlus(10);
        // 扫描索引
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch = ' '; //每次扫描到char保存到ch
        StringBuffer stringBuffer = new StringBuffer();
        while (true) {
            ch = expression.substring(index, index+1).charAt(0);
            if (opers.isOper(ch)) {
                if (!opers.isEmpty()) {
                    // 当前运算符优先级小于之前的优先级，满足则运算
                    if (opers.priority(ch) <= opers.priority(opers.peek())) {
                        num1 = nums.pop();
                        num2 = nums.pop();
                        oper = opers.pop();
                        res = nums.cal(num1, num2, oper);
                        nums.push(res);
                    }
                }
                opers.push(ch);
            } else {
                // 处理多位数字情况
                stringBuffer.append(ch);
                if (index != expression.length()-1) {
                    int step = 0;
                    char charStep;
                    while (true) {
                        // 解决最后一个数字多位
                        if (index + step == expression.length()-1) {
                            break;
                        }
                        charStep = expression.substring(index + step +  1, index + step + 2).charAt(0);
                        if (nums.isOper(charStep)) {
                            break;
                        }
                        step++;
                        stringBuffer.append(charStep);
                    }
                    index = index + step;
                }
                // 对照ask
                nums.push(Integer.parseInt(stringBuffer.toString()));
                stringBuffer.setLength(0);
            }
            index++;
            if (index >= expression.length()) {
                break;
            }
        }

        while (true) {
            if (opers.isEmpty()) {
                System.out.println(expression+ "= " + nums.pop());
                break;
            }
            num1 = nums.pop();
            num2 = nums.pop();
            oper = opers.pop();
            res = nums.cal(num1, num2, oper);
            nums.push(res);
        }
    }
}
class ArrayStackPlus {
    // 最大长度
    private int maxSize;
    // 数组模拟栈
    private int[] stack;
    // 栈顶指针，初始化为-1
    private int top = -1;

    public ArrayStackPlus(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    // 栈顶值，不出栈
    public int peek() {
        return stack[top];
    }

    // 栈满
    public boolean isFull() {
        return top == maxSize - 1;
    }

    // 栈空
    public boolean isEmpty() {
        return top == -1;
    }

    // 入栈
    public void push(int n) {
        if (isFull()) {
            System.out.println("栈满不能插入");
            return;
        }
        top++;
        stack[top] = n;
    }

    // 出栈
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈是空的");
        }
        top--;
        return stack[top+1];
    }

    public void show() {
        if (isEmpty()) {
            System.out.println("栈是空的");
        }
        for (int i = top; i >= 0; i--) {
            System.out.print(stack[i] + "\t");
        }
        System.out.println();
    }

    // 返回运算符优先级,优先级使用数字表示
    public int priority(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1;
        }
    }

    // 判断是不是一个运算符
    public boolean isOper(char val) {
        return val == '+' || val == '-' || val =='*' || val == '/';
    }

    // 计算方法
    public int cal(int num1, int num2, int oper) {
        int res = 0;
        switch (oper) {
            case '+' :
                res = num1 + num2;
                break;
            case '-' :
                res = num2 - num1;
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 / num1;
                break;
        }
        return res;
    }
}